﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
	[Serializable()]
	public class MatHangBO : Csla.BusinessBase<MatHangBO>
	{
		#region Business Properties
		
private int? _MatHangID = null;
public int? MatHangID
{
get { return this._MatHangID; }
set { this._MatHangID = value; PropertyHasChanged("MatHangID"); }
}

private string _MaMatHang = "0";
public string MaMatHang
{
get { return this._MaMatHang; }
set { this._MaMatHang = value; PropertyHasChanged("MaMatHang"); }
}

private string _Ten = null;
public string Ten
{
get { return this._Ten; }
set { this._Ten = value; PropertyHasChanged("Ten"); }
}

private int? _NhomHangID = null;
public int? NhomHangID
{
get { return this._NhomHangID; }
set { this._NhomHangID = value; PropertyHasChanged("NhomHangID"); }
}

private int? _HangSanXuatID = null;
public int? HangSanXuatID
{
get { return this._HangSanXuatID; }
set { this._HangSanXuatID = value; PropertyHasChanged("HangSanXuatID"); }
}

private int? _NuocSanXuatID = null;
public int? NuocSanXuatID
{
get { return this._NuocSanXuatID; }
set { this._NuocSanXuatID = value; PropertyHasChanged("NuocSanXuatID"); }
}

private string _DongMatHang = null;
public string DongMatHang
{
get { return this._DongMatHang; }
set { this._DongMatHang = value; PropertyHasChanged("DongMatHang"); }
}

private string _MoTa = null;
public string MoTa
{
get { return this._MoTa; }
set { this._MoTa = value; PropertyHasChanged("MoTa"); }
}

private string _GhiChu = null;
public string GhiChu
{
get { return this._GhiChu; }
set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
}

private int? _USRC = Session.LoginID;
public int? USRC
{
get { return this._USRC; }
set { this._USRC = value; PropertyHasChanged("USRC"); }
}

private DateTime? _DATC = DateTime.MaxValue;
public DateTime? DATC
{
get { return this._DATC; }
set { this._DATC = value; PropertyHasChanged("DATC"); }
}

private int? _USRM = null;
public int? USRM
{
get { return this._USRM; }
set { this._USRM = value; PropertyHasChanged("USRM"); }
}

private DateTime? _DATM = null;
public DateTime? DATM
{
get { return this._DATM; }
set { this._DATM = value; PropertyHasChanged("DATM"); }
}

private bool? _DELE = false;
public bool? DELE
{
get { return this._DELE; }
set { this._DELE = value; PropertyHasChanged("DELE"); }
}

		private int? _STT0 = 0;
		public int? STT0
		{
			get { return _STT0; }
			set { _STT0 = value; PropertyHasChanged("STT0"); }
		}
		
		protected override object GetIdValue()
		{
			return _MatHangID.Value;
		}
		#endregion Business Properties
		//==========================================================================
		
		#region Factory Methods
		public void SetIsNew()
		{
			MarkNew();
		}

		public void SetIsOld()
		{
			MarkOld();
		}
		
		public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }
		
		private static int newMatHangID =  -1;
private MatHangBO()
{
_MatHangID = newMatHangID--;
}

		public MatHangBO(SqlDataReader dataReader)
		{
			MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public MatHangBO(SqlDataReader dataReader, bool isRoot)
		{
			if (!isRoot)
                MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public static MatHangBO NewRootMatHangBO()
		{
			return DataPortal.Create<MatHangBO>(new RootCriteria());
		}

		internal static MatHangBO NewChildMatHangBO()
		{
			return DataPortal.Create<MatHangBO>(new ChildCriteria());
		}

		public static MatHangBO GetRootMatHangBO(int _MatHangID)
		{
			return DataPortal.Fetch<MatHangBO>(new RootCriteria(_MatHangID));
		}

		public static MatHangBO GetRootMatHangBOByWhere(string where)
		{
			return DataPortal.Fetch<MatHangBO>(where);
		}

		internal static MatHangBO GetChildMatHangBO(SqlDataReader dataReader)
		{
			return new MatHangBO(dataReader);
		}

		public static void DeleteMatHangBO(int _MatHangID)
		{
			DataPortal.Delete(new RootCriteria(_MatHangID));
		}

		public static void DeleteMatHangBO(int _MatHangID, bool directDelete)
		{
			DataPortal.Delete(new RootCriteria(_MatHangID, directDelete));
		}
		#endregion Factory Methods
		//==========================================================================
		
		#region Criteria
		[Serializable()]
		public class RootCriteria
		{
			public int MatHangID;
			public bool directDelete = false;
			public RootCriteria(int _MatHangID)
			{
				this.MatHangID = _MatHangID;
				this.directDelete = false;
			}
			
			public RootCriteria(int _MatHangID, bool directDelete)
            {
                this.MatHangID = _MatHangID;
                this.directDelete = directDelete;
            }
            
			public RootCriteria() {}
		}

		[Serializable()]
		public class ChildCriteria
		{
			public int MatHangID;
			public bool directDelete = false;
			public ChildCriteria(int _MatHangID)
			{
				this.MatHangID = _MatHangID;
				this.directDelete = false;
			}
			
			public ChildCriteria(int _MatHangID, bool directDelete)
            {
                this.MatHangID = _MatHangID;
                this.directDelete = directDelete;
            }
            
			public ChildCriteria() {}
		}
		#endregion Criteria
		//==========================================================================

		#region Create
		[RunLocal]
		private void DataPortal_Create(RootCriteria criteria)
		{
		}

		private void DataPortal_Create(ChildCriteria criteria)
		{
			MarkAsChild();
			ValidationRules.CheckRules();
		}

		#endregion Create
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_Fetch(RootCriteria criteria)
		{
			this.Execute_Fetch(criteria);
		}

		protected void DataPortal_Fetch(string where)
		{
			this.Execute_Fetch(where);
		}

		protected void Execute_Fetch(RootCriteria criteria)
		{
			this.This_Fetch(criteria);
		}
		
		protected void Execute_Fetch(string where)
		{
			this.This_Fetch(where);
		}

		private void This_Fetch(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("MatHangBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[MatHangID] = " + criteria.MatHangID);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void This_Fetch(string where)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("MatHangBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void Fetch(SqlDataReader dataReader)
		{
			if (!(dataReader["MatHangID"] is DBNull))
this._MatHangID = (int)dataReader["MatHangID"];
if (!(dataReader["MaMatHang"] is DBNull))
this._MaMatHang = (string)dataReader["MaMatHang"];
if (!(dataReader["Ten"] is DBNull))
this._Ten = (string)dataReader["Ten"];
if (!(dataReader["NhomHangID"] is DBNull))
this._NhomHangID = (int)dataReader["NhomHangID"];
if (!(dataReader["HangSanXuatID"] is DBNull))
this._HangSanXuatID = (int)dataReader["HangSanXuatID"];
if (!(dataReader["NuocSanXuatID"] is DBNull))
this._NuocSanXuatID = (int)dataReader["NuocSanXuatID"];
if (!(dataReader["DongMatHang"] is DBNull))
this._DongMatHang = (string)dataReader["DongMatHang"];
if (!(dataReader["MoTa"] is DBNull))
this._MoTa = (string)dataReader["MoTa"];
if (!(dataReader["GhiChu"] is DBNull))
this._GhiChu = (string)dataReader["GhiChu"];
if (!(dataReader["USRC"] is DBNull))
this._USRC = (int)dataReader["USRC"];
if (!(dataReader["DATC"] is DBNull))
this._DATC = (DateTime)dataReader["DATC"];
if (!(dataReader["USRM"] is DBNull))
this._USRM = (int)dataReader["USRM"];
if (!(dataReader["DATM"] is DBNull))
this._DATM = (DateTime)dataReader["DATM"];
if (!(dataReader["DELE"] is DBNull))
this._DELE = (bool)dataReader["DELE"];

		}
		#endregion Fetch
		//==========================================================================

		#region Insert
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			this.Execute_Insert();
		}

		protected void Execute_Insert()
		{
			this.This_Insert();
		}

		private void This_Insert()
		{
			this._USRC = Session.LoginID;
this._DATC = DateTime.MaxValue;
this._USRM = null;
this._DATM = null;
this._DELE = false;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("MatHangBO_InsertBO");
			db.AddInParameter(cmd, "@MaMatHang", SqlDbType.NVarChar, this._MaMatHang);
db.AddInParameter(cmd, "@Ten", SqlDbType.NVarChar, this._Ten);
db.AddInParameter(cmd, "@NhomHangID", SqlDbType.Int, this._NhomHangID);
db.AddInParameter(cmd, "@HangSanXuatID", SqlDbType.Int, this._HangSanXuatID);
db.AddInParameter(cmd, "@NuocSanXuatID", SqlDbType.Int, this._NuocSanXuatID);
db.AddInParameter(cmd, "@DongMatHang", SqlDbType.NVarChar, this._DongMatHang);
db.AddInParameter(cmd, "@MoTa", SqlDbType.NVarChar, this._MoTa);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
db.AddOutParameter(cmd, "@NewMatHangID", SqlDbType.Int, 10);
db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			this._MatHangID = (int)cmd.Parameters["@NewMatHangID"].Value;
this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
		}
		#endregion Insert
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			this.Execute_Update();
		}

		protected void Execute_Update()
		{
			this.This_Update();
		}

		private void This_Update()
		{
			this._USRM = Session.LoginID;
this._DATM = DateTime.Now;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("MatHangBO_UpdateBO");
			db.AddInParameter(cmd, "@MatHangID", SqlDbType.Int, this._MatHangID);
db.AddInParameter(cmd, "@MaMatHang", SqlDbType.NVarChar, this._MaMatHang);
db.AddInParameter(cmd, "@Ten", SqlDbType.NVarChar, this._Ten);
db.AddInParameter(cmd, "@NhomHangID", SqlDbType.Int, this._NhomHangID);
db.AddInParameter(cmd, "@HangSanXuatID", SqlDbType.Int, this._HangSanXuatID);
db.AddInParameter(cmd, "@NuocSanXuatID", SqlDbType.Int, this._NuocSanXuatID);
db.AddInParameter(cmd, "@DongMatHang", SqlDbType.NVarChar, this._DongMatHang);
db.AddInParameter(cmd, "@MoTa", SqlDbType.NVarChar, this._MoTa);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			
this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
		}
		#endregion Update
		//==========================================================================

		#region Delete
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new RootCriteria(_MatHangID.Value));
		}

		private void DataPortal_Delete(RootCriteria criteria)
		{
			this.Execute_Delete(criteria);
		}

		protected void Execute_Delete(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("MatHangBO_DeleteBO");
			db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
			db.AddInParameter(cmd, "@MatHangID", SqlDbType.Int, criteria.MatHangID);
			db.ExecuteNonQuery(cmd);
		}
		#endregion Delete
		//==========================================================================

		#region Internal method
		internal void Insert()
		{
			if (!this.IsDirty) return;
			DataPortal_Insert();
			MarkOld();
		}

		internal void Update()
		{
			if (!this.IsDirty) return;
			DataPortal_Update();
			MarkOld();
		}

		internal void DeleteSelf()
		{
			if (!this.IsDirty) return;
			if (this.IsNew) return;
			DataPortal_Delete(new RootCriteria(_MatHangID.Value));
			MarkNew();
		}
		#endregion Internal method
		//==========================================================================
	}
}
